<template>
  <div class="login">
    <div class="login-box">
      <div class="title">
        <p>润云云盘</p>
      </div>
      <a-form :form="userForm" class="login-form">
        <a-form-item>
          <a-input v-model="userForm.username" placeholder="账号">
            <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            v-model="userForm.password"
            type="password"
            placeholder="密码"
          >
            <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            class="code-input"
            v-model="userForm.code"
            placeholder="验证码"
            :maxLength="4"
          >
            <a-icon slot="prefix" type="robot" style="color: rgba(0,0,0,.25)" />
          </a-input>
          <img
            class="code-img"
            @click="getAuthCode"
            :src="authCode"
            alt="验证码"
          />
        </a-form-item>
        <a-form-item style="text-align: center">
          <a-button
            type="primary"
            class="login-form-button"
            @click="loginButtonClick"
          >
            登录
          </a-button>
          <a-button class="login-form-button" @click="registerButtonClick">
            注册
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userForm: {
        username: '',
        password: '',
        code: '',
        uuid: ''
      },
      authCode: '',
      codeString: ''
    }
  },
  methods: {
    // 登录按钮
    loginButtonClick() {
      if (
        this.userForm.password.trim() === '' ||
        this.userForm.username.trim() === ''
      ) {
        return this.$message.info('密码和用户名都不能为空')
      }
      // console.log(this.codeString, this.authCode)
      if (this.userForm.code !== this.codeString) {
        return this.$message.error('验证码错误')
      }
      this.$api.user.login(this.userForm).then((res) => {
        this.$message.success('登录成功')
        this.$utils.setToken(res.token)
        this.$router.push('/files')
      })
    },
    // 注册按钮
    registerButtonClick() {
      this.$router.push('register')
    },
    // 获取验证码
    getAuthCode() {
      this.$api.user.getAuthCode().then((res) => {
        this.authCode = res.img
        this.code = res.code
        this.userForm.uuid = res.uuid
        this.codeString = res.codeString
      })
    }
  },
  mounted() {
    this.getAuthCode()
  }
}
</script>
<style lang="less" scoped>
.login {
  background: url('../assets/backgroud/login_bg.jpg') no-repeat;
  background-size: cover;
  position: fixed;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  .login-box {
    background-color: rgba(255, 255, 255, 0.95);
    position: relative;
    z-index: 3;
    max-width: 100%;
    width: 350px;
    margin: 120px auto 0px;
    padding: 30px 50px 30px 50px;
    border-radius: 5px;
    .title {
      font-size: 25px;
      color: #4c6c79;
      text-align: center;
    }
    .login-form {
      max-width: 300px;
      padding-top: 15px;
      .code-input {
        width: 100px;
      }
      .code-img {
        padding-left: 30px;
        width: 60%;
        height: 100%;
      }
    }
    .login-form-button {
      width: 80%;
      margin-top: 20px;
    }
  }
}
</style>
